home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Add-Ons / After Dark / Joe Judge / Blot ƒ / blot.c < prev    next >
Encoding:
Text File  |  1994-11-26  |  4.8 KB  |  213 lines  |  [TEXT/KAHL]

  1.  
  2. // to-do ... make it use RGBColor instead of indexed color
  3. //  since all of my util routines use RGBColor
  4. // // //
  5. // ad-ized by j.t.judge
  6. // May 14, 1994
  7.  
  8. /*
  9.  * Rorschach's ink blot test
  10.  *
  11.  * Copyright (c) 1992 by Jamie Zawinski
  12.  *
  13.  * 2-Sep-93: xlock version (David Bagley bagleyd@source.asset.com)
  14.  * 1992:     xscreensaver version (Jamie Zawinski jwz@lucid.com)
  15.  */
  16.  
  17. /* original copyright
  18.  * Copyright (c) 1992 by Jamie Zawinski
  19.  *
  20.  * Permission to use, copy, modify, distribute, and sell this software and its
  21.  * documentation for any purpose is hereby granted without fee, provided that
  22.  * the above copyright notice appear in all copies and that both that
  23.  * copyright notice and this permission notice appear in supporting
  24.  * documentation.  No representations are made about the suitability of this
  25.  * software for any purpose.  It is provided "as is" without express or
  26.  * implied warranty.
  27.  */
  28.  
  29. //#include "xlock.h"
  30.  
  31. #include "blot.h"
  32. #include "utils.h"
  33.  
  34. typedef struct {
  35.     int width;
  36.     int height;
  37.     int xmid, ymid;
  38.     int offset;
  39.     Boolean xsym, ysym;
  40.     int size;
  41.     unsigned short pix;                    // index into the color map (which color to use)
  42.     unsigned long startTime;    // seconds
  43. } blotstruct;
  44.  
  45.  
  46.  
  47. static blotstruct blots[MAXSCREENS];
  48.  
  49. #if 0
  50. //static XPoint *pointBuffer = 0; /* pointer for XDrawPoints */
  51. static Point *pointBuffer = 0;
  52. #else
  53. static Point pointBuffer[ MAXPOINTS+1 ];    //  640 * 480 / 1024 = 6000 (??)
  54. #endif
  55.  
  56. short gMaxpoints;
  57. short gTimeout;
  58. //#define TIMEOUT 30
  59. Boolean gXsym, gYsym;
  60. short gOffset;
  61.  
  62. int
  63. initblot(int whichScreen, Rect *theRect, short screenDepth)
  64. {
  65.     blotstruct *bp = &blots[whichScreen];
  66.  
  67.     if (whichScreen >= MAXSCREENS)    // lets not tromp on memory
  68.         return;
  69.         
  70.         
  71.     bp->width = theRect->right - theRect->left;
  72.     bp->height = theRect->bottom - theRect->top;
  73.     bp->xmid = bp->width / 2;
  74.     bp->ymid = bp->height / 2;
  75.  
  76.     bp->offset = gOffset;    // 4;
  77.     bp->xsym = gXsym;        //TRUE;
  78.     bp->ysym = gYsym;        //FALSE;
  79. #if 0
  80.     bp->pix = 0;
  81. #else
  82.     bp->pix = RangedRdm( 0, (1<<screenDepth) );
  83. #endif    
  84.     if (bp->offset <= 0) bp->offset = 3;
  85.  
  86. #if 0 // lets take this out  - it's not like I want to use lots of memory + cpu
  87.       // to draw large chunks of points at a time. 
  88.       
  89.     if (batchcount >= 100 || batchcount < 1)
  90.       batchcount = 6;
  91.     /* Fudge the size so it takes up the whole screen */
  92.     bp->size = batchcount * bp->width * bp->height / 1024;
  93.  
  94.     if (!pointBuffer)
  95.         pointBuffer = (Point *) NewPtr(bp->size * sizeof(Point));
  96.     if (!pointBuffer) return -1;
  97. #endif
  98.     bp->size = gMaxpoints;        //MAXPOINTS;
  99.  
  100.     ForeColor( blackColor );
  101.     BackColor( blackColor);
  102.     EraseRect( theRect);
  103.     ForeColor( whiteColor );
  104.     GetDateTime( &bp->startTime );
  105. }
  106.  
  107. void
  108. drawblot(int whichScreen, Rect *theRect, short screenDepth) 
  109. {
  110.   int x, y;
  111.   int         k;
  112.   Point     *xp = pointBuffer;
  113.   blotstruct *bp = &blots[whichScreen];
  114.   unsigned long secs;
  115.  
  116.  
  117.     if (whichScreen >= MAXSCREENS)    // lets not tromp on memory
  118.         return;
  119.  
  120.     bp->offset = gOffset;    // 4;
  121.     bp->xsym = gXsym;        //TRUE;
  122.     bp->ysym = gYsym;        //FALSE;
  123.     bp->size = gMaxpoints;        //MAXPOINTS;
  124.  
  125. #if 0
  126.     if (screenDepth > 8) {
  127.         RandomRGBWalk( &bp->pix );        // ignore bp->pix and choose any color
  128.     } else if (screenDepth > 1) {
  129.         SetIndexedColor( bp->pix);
  130.         if (++bp->pix >=  (1 << screenDepth)  )        // 1<<screenDepth == howManyColors
  131.             bp->pix = 0;
  132.         bp->pix = RangedRdm( 0, (1<<screenDepth) );
  133.     }
  134. #else
  135.     if (screenDepth > 1) {
  136.         if (!gTimeout) {                    // no timeout?  brights and darks
  137.             if (!(Random()%2))
  138.                 bp->pix = PickDarkIndexColor();
  139.             else
  140.                 bp->pix = PickBrightIndexColor( );
  141.         } else 
  142.             bp->pix = PickBrightIndexColor();    // brights only    
  143.         
  144.         // now ... set our forecolor to that new color
  145.         SetIndexedColor( bp->pix);        
  146.     }
  147. #endif
  148.  
  149.  
  150.   x = bp->xmid;
  151.   y = bp->ymid;
  152.   k = bp->size;
  153.   
  154.     while (k >= 4) {
  155.         x += ((random () % (1 + (bp->offset << 1))) - bp->offset);
  156.         y += ((random () % (1 + (bp->offset << 1))) - bp->offset);
  157.         k--;
  158.         xp->h = x;
  159.         xp->v = y;
  160.         xp++;
  161.         if (bp->xsym) {
  162.             k--;
  163.             xp->h = bp->width - x;
  164.             xp->v = y;
  165.             xp++;
  166.         }
  167.         
  168.         if (bp->ysym) {
  169.             k--;
  170.             xp->h = x;
  171.             xp->v = bp->height - y;
  172.             xp++;
  173.         }
  174.         if (bp->xsym && bp->ysym) {
  175.             k--;
  176.             xp->h = bp->width - x;
  177.             xp->v = bp->height - y;
  178.             xp++;
  179.         }
  180.     }
  181.  
  182.     XDrawPoints ( theRect, pointBuffer, bp->size - k);
  183.     if (gTimeout) {
  184.         GetDateTime( &secs );
  185.         if ( (short)(secs - bp->startTime) > gTimeout)
  186.             initblot(whichScreen, theRect, screenDepth );
  187.     }
  188. }
  189.  
  190.  
  191.  
  192. // draw a set of points .... 
  193. void
  194. XDrawPoints(Rect *theRect, Point *points, short how_many) {
  195.  
  196.     while (how_many-- > 0) {
  197.     
  198.         if ( (theRect->left + points[how_many].h) > theRect->right) 
  199.             continue;
  200.         if ( (theRect->top + points[how_many].v) > theRect->bottom)
  201.             continue;
  202.         
  203.         MoveTo( theRect->left + points[how_many].h, 
  204.                 theRect->top + points[how_many].v );
  205.         LineTo( theRect->left + points[how_many].h, 
  206.                 theRect->top + points[how_many].v );
  207.     }
  208.  
  209. }
  210.  
  211.  
  212.  
  213.